<?php

/**
 * @Author: 刘洪
 * @Date:   2018-11-21 11:49:28
 * @Last Modified by:   user
 * @Last Modified time: 2019-01-04 11:33:16
 */

namespace app\common\remind;

use app\common\remind\RemindMain;
use think\Db;
use think\Log;

/**
 * 合同提醒类
 */
class RemindContract extends RemindMain
{
	
	/**
	 * 	设置提醒类型   合同提醒
	 * @var integer
	 */
	protected $remindType=2;

	/**
	 * 合同提醒类主入口方法
	 * @return [type] [description]
	 */
	public function index(){
		//初始化
		$this->remindList=parent::init();
		//echo '合同提醒执行';
		//数据重组
		if(true==$this->is_first){
			//echo '测试';
			$this->regroupData();
			//发送提醒
			parent::send('合同');
		}
	}
		/**
	 * 实现父类抽象方法
	 * @access protected
	 * @return [type] [description]
	 */
	protected function regroupData()
	{
		//arr[]=[trigger,send_type,receives,send_connect]
		//		[触发类型[]，发送方式，接收人[]，发送内容[]]
		foreach ($this->remindList as $arr) {
			//dump($arr['receives']);
			//获取触发条件
			$endbefore=$arr['trigger'][0];
			$HZ=$arr['trigger'][1];


			//提醒条件  在设定条件上 再加上 超期30天的也要提醒     异常合同和关闭的合同不提醒   
			$where="select * from cmf_hetong where timestampdiff(day,curdate(),from_unixtime(end_time,'%Y-%m-%d'))<=".$endbefore." and timestampdiff(day,curdate(),from_unixtime(end_time,'%Y-%m-%d'))%".$HZ."=0 and timestampdiff(day,curdate(),from_unixtime(end_time,'%Y-%m-%d'))>=-30 and status<>2 and status<>3";
				//unix_timestamp(now())<=end_time
			//获取合同
			$contract=Db::query($where);
			//dump($contract);
			//格式化数据
			$this->formatSendList($contract,$arr);
			
		}
	}


	/**
	 * 数据格式化
	 * @access protected
	 * @param  array $contract [description]
	 * @param  array $arr      [description]
	 * @return [type]           [description]
	 */
	protected function formatSendList($contract,$arr)
	{
		//arr[]=[trigger,send_type,receives,send_connect]
			//[触发类型，发送方式，接收人，发送内容]
		
		foreach ($contract as $it) {

			//send_connect[合同编号0，名称1，到期时间2，客户名称3，联系人4，客户手机5]
			$send_connect="<div style='background-color:#ccc;margin-top:10px;'><h3>合同到期提醒</h3>";
			$sms_connect="(";
			foreach ($arr['send_connect'] as $k) {
				if($k==0){
					$send_connect.="<div>合同编号：".$it['contract_num']."</div>";
					$sms_connect.='编号：'.$it['contract_num'];
				}
				if($k==1){
					$send_connect.="<div>合同名称：".$it['title']."</div>";
					$sms_connect.=" 名称：".$it['title'];
				}
				if($k==2){
					$today=date('Ymd',time());
					$endtime=date("Ymd",$it['end_time']);
					$day=(strtotime(date("Ymd",$it['end_time']))-strtotime('today'))/86400;
					$send_connect.="<div>到期时间：".date("Y-m-d",$it['end_time'])." 还有".$day."天</div>";
					$sms_connect.=" 距离".date("Y-m-d",$it['end_time'])."到期还有：".$day."天";
				}
				if($k==3){
					$send_connect.="<div>客户名称：".$it['kh_name']."</div>";
				}
				if($k==4){
					$send_connect.="<div>联系人：".$it['username']."</div>";
				}
				if($k==5){
					$send_connect.="<div>客户手机：".$it['mobile']."</div>";
				}
			}

			$send_connect.="</div>";
			$sms_connect.=");";



			//echo '收件人';
			//dump($arr['receives']);
			//接收人 
			if($arr['receives'][0]==0){//签订人
				$receives="";
				$user=Db::name('user')->where('id='.$it['user_id'])->find();
				//邮箱发送
				if($arr['send_type']==0 || $arr['send_type']==1){
					if(!isset($this->sendList['email'][$user['user_email']])){
						$this->sendList['email'][$user['user_email']]="";
					}
					$this->sendList['email'][$user['user_email']].=$send_connect;
				}

				//短信发送
				if($arr['send_type']==0 || $arr['send_type']==2){
					if(!isset($this->sendList['mobile'][$user['mobile']])){
						$this->sendList['mobile'][$user['mobile']]="";
					}
					$this->sendList['mobile'][$user['mobile']].=$sms_connect;
				}

			}else if($arr['receives'][0]==1){//客户

				$today=date('Ymd',time());
				$day=(strtotime(date("Ymd",$it['end_time']))-strtotime('today'))/86400;
				$t=explode('-',date("Y-m-d",$it['end_time']));
				$date=$t[0].'年'.$t[1].'月'.$t[2].'日';
				//dump($it['username']);
				
				$name=mb_substr($it['username'],0,1,'utf-8');
				//dump(mb_strlen($it['username'],'utf8'));
				for ($i=1; $i <mb_strlen($it['username'],'utf8'); $i++) { 
					$name.="*";
				}
				$name.="先生(女士)";
				//dump($name);
				$mobile=$it['mobile'];
				//dump($mobile);
				$connect='{"name":"'.$name.'","product":"('.$it['title'].'合同)","date":"'.$date.'","day":"'.$day.'"}';
				Log::write("电话：".$mobile."发送内容：".$connect);
				//'{"name":"先生（女士）","product":"官网系统开发项目合同","date":"2019年10月10日","day":"5"}'
				//dump($connect);
				//
				
				$res=sms_send($mobile,$connect,'SMS_154585568');
				Log::write("发送结果：".json_decode($res));
				//dump($res);
				sleep(3);
			}

		}
	}

}
